package com.smart.car.member.controller;

import com.smart.car.carwash.api.service.CarWashProviderService;
import com.smart.car.common.res.bean.ResponseResult;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.member.entity.MemberCard;
import com.smart.car.member.entity.MemberVehicle;
import com.smart.car.member.service.MemberCardService;
import com.smart.car.member.service.MemberVehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆模块控制
 * @author: zoutao
 * @date: 2021/4/15
 */
@Api(tags = "车辆模块")
@Slf4j
@RestController
@RequestMapping("/vehicle")
public class MemberVehicleController {

    @Autowired
    private MemberVehicleService vehicleService;

    /**
     * TODO 查询所有车辆信息
     * @author zoutao.blog.csdn.net
     */
    @ApiOperation(value = "查询所有车辆")
    @GetMapping("/list")
    public List<MemberVehicle> list() {
        List<MemberVehicle> members = vehicleService.list();
        log.info("查询车辆列表：{} " ,members);
        return members;
    }

    /**
     * TODO 添加车辆信息
     * @param json
     * @return
     */
    @PostMapping(value = "/add")
    public ResponseResult<Integer> addVehicle(String json) throws BusinessException {
        log.debug("添加车辆信息： " + json);
        ResponseResult<Integer> result = new ResponseResult<>();
        int rtn = vehicleService.addVehicle(json);
        result.setData(rtn);
        return result;
    }

    /**
     * TODO 根据用户id查询车辆信息
     * @author zoutao.blog.csdn.net
     */
    @PostMapping(value = "/get")
    public ResponseResult<MemberVehicle> get(String memberId) throws BusinessException {
        log.debug("根据用户id查询车辆信息： " + memberId);
        MemberVehicle rtn = vehicleService.getVehicle(memberId);
        return ResponseResult.ok(rtn);
    }


    //像调用本地jar一样，调用dubbo服务：需要保证CarwashProvider服务启动。
    //@Reference
    private CarWashProviderService washService;

    /**
     * TODO Dubbo 调用示例--洗车服务
     * TODO {"plateNum":"粤AG9527","couponCode":"Ts0999"}
     * @param json
     * @return result
     */
    @PostMapping("/wash")
    public ResponseResult<Integer> wash(@RequestBody String json) throws BusinessException {
        log.debug("洗车服务 = " + json);
        ResponseResult<Integer> result = new ResponseResult<>();
        int rtn = washService.wash(json);
        result.setCode(200);
        result.setMsg("dubbo调用，洗车业务开展成功");
        result.setData(rtn);
        return result;
    }

}